package com.liu.network.ip.bean;

/**
 * 读写文件
 */
import java.io.*;

import net.sf.json.JSONObject;

public class FileUtil {
	private String filePath = "";

	public String getFilePath() {
		return filePath;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

	public FileUtil() {
		super();
	}

	public FileUtil(String filePath) {
		super();
		this.filePath = filePath;
	}

	public File initFile() {
		File file = new File(filePath);
		// 获取父目�?
		String dirs = file.getParent();
		File fileDirs = new File(dirs);
		// 路径中的目录是否存在�?
		if (!fileDirs.exists()) {
			boolean bMkdirs = fileDirs.mkdirs();
			System.out.println("创建不存在的父目�?" + dirs + "\t创建结果�?" + bMkdirs);
		}

		// 路径中的文件是否存在�?
		if (!file.exists()) {
			boolean bCreateNewFile = false;
			try {
				bCreateNewFile = file.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println("创建新的空文�?" + bCreateNewFile);
		}
		return file;
	}

	public File initFile(String filePath) {
		File file = new File(filePath);
		// 获取父目�?
		String dirs = file.getParent();
		File fileDirs = new File(dirs);
		// 路径中的目录是否存在�?
		if (!fileDirs.exists()) {
			boolean bMkdirs = fileDirs.mkdirs();
			System.out.println("创建不存在的父目�?" + dirs + "\t创建结果�?" + bMkdirs);
		}

		// 路径中的文件是否存在�?
		if (!file.exists()) {
			boolean bCreateNewFile = false;
			try {
				bCreateNewFile = file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
			System.out.println("创建新的空文�?" + bCreateNewFile);
		}
		return file;
	}

	public static void main(String[] args) throws IOException {
		String inFilePath = "C:\\Users\\12961\\Desktop\\coordinate.json";
		String outFilePath = "C:\\Users\\12961\\Desktop\\new\\bb.txt";
		FileUtil fileUtil = new FileUtil();
		/**
		 * 字节�?
		 */
		// 读取
		fileUtil.fileInStream(fileUtil.initFile(inFilePath));
		/**
		 * 字符流（纯文本）
		 */
		// 读取
//		fileUtil.fileReaderByChar(fileUtil.initFile(inFilePath));
//		fileUtil.fileReaderByLine(fileUtil.initFile(inFilePath));
		// 写入
//		fileUtil.fileWriter(fileUtil.initFile(outFilePath), true, "true续写，false覆盖");
	}

	public void fileInStream(File file) {
		StringBuffer str = new StringBuffer();
		FileInputStream is = null;
		BufferedInputStream bis = null;
		try {
			is = new FileInputStream(file);
			bis = new BufferedInputStream(is);
			int len = 0;
			byte[] be = new byte[1024];
			while ((len = bis.read(be)) != -1) {
				str.append(new String(be, 0, len));
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (bis != null) {
				try {
					bis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		JSONObject jo = JSONObject.fromObject(str.toString());
		for (int i = 0; i < jo.size(); i++) {
			jo.get("北京");
		}
	}

	/**
	 * 将数据写入文件中
	 * 
	 * @param file
	 *            输出文件
	 * @param append
	 *            是否保存之前的内�?
	 * @param data
	 *            输出数据
	 */
	public void fileWriter(File file, boolean append, String data) {
		FileWriter fileWriter = null;
		try {
			fileWriter = new FileWriter(file, append);
			fileWriter.write(data);
			// 输出流进行刷新，将字符写到目的文件中�?
			fileWriter.flush();
			fileWriter.write("\n" + data);
			// 输出流进行刷新，将字符写到目的文件中�?
			fileWriter.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("写入数据失败");
		} finally {
			try {
				if (fileWriter != null) {
					fileWriter.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	/**
	 * 字符流读取文本：�?行行读取
	 * 
	 * @param file
	 */
	public void fileReaderByLine(File file) {
		StringBuffer str = new StringBuffer();
		FileReader fr = null;
		BufferedReader br = null;
		try {
			fr = new FileReader(file);
			br = new BufferedReader(fr);
			String s = null;
			while ((s = br.readLine()) != null) {
				str.append(s + "\n");
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (br != null) {
					br.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				if (fr != null) {
					fr.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		System.out.println(str.toString());
	}

	/**
	 * 字符流读取文本：�?个个字符读取
	 * 
	 * @param file
	 */
	public void fileReaderByChar(File file) {
		StringBuffer str = new StringBuffer();
		char[] cr = new char[1024];
		FileReader fr = null;
		try {
			fr = new FileReader(file);
			int it = -1;
			while ((it = fr.read(cr)) != -1) {
				str.append(cr);
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (fr != null) {
					fr.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		System.out.println(str.toString());
	}
}
